package com.example.demo.controller;

import com.example.demo.model.Student;
import com.example.demo.repository.StudentRepository;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.*;

@Controller
@RequestMapping("student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    public static Map<String, Student> students = new HashMap<>();

    static {
        Student s1 = new Student("1", "tony");
        students.put(s1.getId(), s1);
        Student s2 = new Student("2", "jerry");
        students.put(s2.getId(), s2);
    }

    @RequestMapping("/list")
    public String search(ModelMap map, Student student) {
//        List<Student> students =  studentRepository.findByNameOrAge(student.getName(),student.getAge());
        if ("".equals(student.getName())) {
            student.setName(null);
        }
        List<Student> students = studentService.findAll(student);
//        List<Student> students = studentRepository.findByNativeQuery(student.getName());

        map.put("studentList", students);

        return "student/list";
    }

    //    @RequestMapping("list")
    public String list(ModelMap map, Student student) {
//        System.out.println(123);
//        map.put("studentList", students.values());
        if ("".equals(student.getName())) {
            student.setName(null);
        }
//        List<Student> students = studentRepository.findAll(Example.of(student));

        map.put("studentList", studentService.findAll(student));

        return "list";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable String id) {
//        System.out.println("id:" + id);
//        int index = Integer.parseInt(id) - 1;
//        students.remove(index);
//        students.remove(id);//map

        studentService.deleteById(id);//数据库删除
        return "redirect:/student/list";//重定向
//        return "forward:/list";//转发
    }

    @RequestMapping("create")
    public String create() {
        return "/student/create";
    }

    @RequestMapping("save")
    public String save(Student s) {
        if (s.getId() == null) {
            s.setId(UUID.randomUUID().toString());
        }
//        students.put(s.getId(), s);
        studentService.save(s);//数据库保存
        return "redirect:/student/list";
    }

    @RequestMapping("update/{id}")
    public String update(@PathVariable String id, ModelMap map) {
//        Student s = students.get(String.valueOf(Integer.parseInt(id) - 1));
//        map.put("student", s);
//        Student s = students.get(id);
        Student s = studentService.getReferenceById(id);//数据库查询
        map.put("student", s);
        return "/student/update";
    }
}

